package com.situ.mall.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.situ.mall.common.entity.Member;
import com.situ.mall.common.exception.MyException;
import com.situ.mall.common.mapper.MemberMapper;
import com.situ.mall.common.service.IMemberService;
import com.situ.mall.common.util.RequestUtil;
import com.situ.mall.common.util.StringUtil;
import com.situ.mall.common.vo.ResultVO;
import com.situ.mall.common.vo.TableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 闫佳宁
 * @since 2023-09-21
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public TableVO select(Integer page, Integer limit, String username, String name, Integer status) {
        //pageHelper 分页小工具
        PageHelper.startPage(page, limit);
        //上面这行代码后跟的第一次查数据库是分页查询，只生效一次
        return TableVO.data(memberMapper.select(username, name, status));
    }

    @Override
    public void update(Integer id) {
        memberMapper.update(id);
    }

    @Override
    public void updateById1(Member m) {
        baseMapper.update1(m);
    }

    @Override
    public Member selectByTel(String tel) {
        return memberMapper.selectByTel(tel);
    }

    @Override
    public void saveMember(String tel, String pas, String uname) {
        Member m = new Member();
        LocalDateTime ct = m.setCreateTime(LocalDateTime.now()).getCreateTime();
        memberMapper.saveMember(tel,StringUtil.md5(pas),uname,ct);
    }

    @Override
    public TableVO UpdatePassword(String oldPassword, String newPassword, String newPasswordAgain, HttpServletRequest request, HttpServletResponse response) {
        TableVO vo = new TableVO();
        Member member =(Member)RequestUtil.getCurrent();
        if (member != null && !member.getPassword().equals(StringUtil.md5(oldPassword))) {
            vo.setCode(2);
            vo.setMsg("原始密码输入错误");
            return vo;
        }
        if (!newPassword.equals(newPasswordAgain)) {
            vo.setCode(2);
            vo.setMsg("两次输入输入不一致");
            return vo;
        }
        if (newPassword.length() > 16 || newPassword.length() < 6) {
            vo.setCode(2);
            vo.setMsg("修改失败，密码应在6-16位");
            return vo;
        }
        vo.setCode(0);
        vo.setMsg("密码修改成功");
        request.getSession().getAttribute("currentUser");
        if (member != null) {
            memberMapper.UpdatePassword(StringUtil.md5(newPassword), member.getId());
        }
        return vo;
    }

    @Override
    public boolean check(String username, String password) {
        Member m = baseMapper.selectOne(new LambdaQueryWrapper<Member>().eq(Member::getUsername, username));
        boolean b = m != null && m.getPassword().equals(StringUtil.md5(password));
        if (b) {
            //把当前用户保存进session
            RequestUtil.setCurrent(m);
        }
        return b;
    }

}
